#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>

using namespace dlib;
using namespace std;

int main(int argc, char** argv) {

    //must be a parameter : image_path. check it ,and get image_path from argv
    if (argc != 2) {
        std::cout << "Usage: ./face_detector <image_path>" << std::endl;
        return -1;
    }

    //define image_path and set the value from argv[1]
    string image_path = argv[1];

    // 初始化人脸检测器
    frontal_face_detector detector = get_frontal_face_detector();

    // 加载人脸特征点检测器
    shape_predictor sp;
    deserialize("/home/hhy212306170/practice/model/shape_predictor_5_face_landmarks.dat") >> sp;

    // 加载图像
    array2d<rgb_pixel> img;
    load_image(img, image_path);

    // 检测人脸
    std::vector<rectangle> dets = detector(img);

    // 对每个检测到的人脸进行特征点检测
    std::vector<full_object_detection> shapes;
    for (auto& d : dets) {
        full_object_detection shape = sp(img, d);
        shapes.push_back(shape);
    }

    // 显示图像和检测到的人脸特征点
    image_window win;
    win.clear_overlay();
    win.set_image(img);
    win.add_overlay(render_face_detections(shapes)); // 绘制特征点

    // 保持窗口打开
    cout << "Press enter to exit." << endl;
    cin.get();
    return 0;
}